#include <QCoreApplication>
#include <event2/event.h>
#include <event2/listener.h>
#include <iostream>
#include <signal.h>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    if(signal(SIGPIPE,SIG_IGN) == SIG_ERR){
        return -1;
    }

    //创建配置上下文
    event_config* conf = event_config_new();
    //显示支持的网络模式
    const char** methods = event_get_supported_methods();
    qDebug() << "支持的网络模式有:";
    for(int i = 0;methods[i] != NULL;++i){
        std::cout << methods[i] << std::endl;
    }
//    //设置网络模型，使用select
//    event_config_avoid_method(conf,"epoll");
//    event_config_avoid_method(conf,"poll");
//    //如果使用select，则只支持开启fds特征
//    event_config_require_features(conf,EV_FEATURE_FDS);
    //设置特征，默认配置除了fds外都开启.如果开启fds,则网络模型不支持epoll
    event_config_require_features(conf,EV_FEATURE_ET | EV_FEATURE_O1);



    //用配置创建libevent上下文
    event_base* base = event_base_new_with_config(conf);
    event_config_free(conf);

    if(!base){
        std::cerr << "event_base_new_with_config fail" << std::endl;
    }else{
        //获取当前使用的网络模型
        qDebug() << "current methods is:" << event_base_get_method(base);


        //获取当前base设置的特征
        //确认特征是否生效
        int ret = event_base_get_features(base);
        if(ret & EV_FEATURE_ET){
            qDebug() << "EV_FEATURE_ET";
        }
        if(ret & EV_FEATURE_O1){
            qDebug() << "EV_FEATURE_O1";
        }
        if(ret & EV_FEATURE_FDS){
            qDebug() << "EV_FEATURE_FDS";
        }
        if(ret & EV_FEATURE_EARLY_CLOSE){
            qDebug() << "EV_FEATURE_EARLY_CLOSE";
        }


        std::cout << "event_base_new_with_config success" << std::endl;
        event_base_free(base);
    }

    return a.exec();
}
